define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facilitators/index' + location.search,
                    add_url: 'facilitators/add',
                    edit_url: 'facilitators/edit',
                    del_url: 'facilitators/del',
                    multi_url: 'facilitators/multi',
                    import_url: 'facilitators/import',
                    table: 'facilitators',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'sms_other', title: __('Sms_other')},
                        {field: 'is_open', title: __('Is_open'), searchList: {"0":__('Is_open 0'),"1":__('Is_open 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //服务商和校区等页面
        moretable:function (){
            // 初始化表格参数配置
            Table.api.init();
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        //多表格
        table: {
            //服务商管理
            facilitators: function () {
                var facilitators_table = $("#facilitators_table");
                facilitators_table.bootstrapTable({
                    url: 'facilitators/index' + location.search,
                    extend: {
                        index_url: 'facilitators/index' + location.search,
                        add_url: 'facilitators/add',
                        // edit_url: 'facilitator/change_true',
                        del_url: 'facilitators/del',
                        multi_url: 'facilitators/multi',
                        import_url: 'facilitators/import',
                        table: 'facilitators',
                    },
                    toolbar: '#facilitators_toolbar',
                    pk: 'id',
                    sortName: 'id',
                    fixedColumns: true,
                    fixdedRightNumber: 1,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'facilitator.title', title: __('客户名称')},
                            {field: 'facilitator.identity_sn', title: __('身份证/机构证件号')},
                            {field: 'brand.name', title: __('品牌')},
                            {field: 'facilitator.name', title: __('联系人')},
                            {field: 'facilitator.mobile', title: __('手机号码')},
                            {field: 'facilitator.area', title: __('服务区域')},
                            {field: 'store.nickname', title: __('登录账户')},
                            {field: 'school_num', title: __('校区数')},
                            {
                                field: 'is_open',
                                title: __('天育系统'),
                                searchList: {
                                    "0": __('未开通'),
                                    "1": __('已开通'),
                                },
                                formatter: Table.api.formatter.normal
                            },
                            {field: 'status', title: __('状态'), searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
                            {field: 'operate', title: __('Operate'), table: facilitators_table, buttons: [
                                    {
                                        name: 'change_facilitator',
                                        text: '编辑',
                                        title: '编辑',
                                        extend: 'data-area=["70%","70%"]',
                                        classname: 'btn btn-xs btn-primary btn-dialog',
                                        url: function (row) {
                                            return 'facilitator/change_true/ids/' + row.facilitator.id;
                                        },

                                    },
                                    {
                                        name: 'ebank',
                                        text: '网银账户',
                                        title: '网银账户',
                                        extend: 'data-area=["70%","70%"]',
                                        classname: 'btn btn-xs btn-primary btn-dialog',
                                        url: function (row) {
                                            return 'ebank/index/shop_id/' + row.id;
                                        },

                                    },

                                ], events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ],
                });
                //为表格绑定事件
                Table.api.bindevent(facilitators_table);
            },
            //学校管理
            school: function () {
                var school_table = $("#school_table");
                school_table.bootstrapTable({
                    url: 'facilitators/school' + location.search,
                    extend: {
                        index_url: 'facilitators/school' + location.search,
                        add_url: 'facilitators/add',
                        edit_url: 'facilitators/edit',
                        del_url: 'facilitators/del',
                        multi_url: 'facilitators/multi',
                        import_url: 'facilitators/import',
                        table: 'school',
                    },
                    toolbar: '#school_toolbar',
                    pk: 'id',
                    sortName: 'id',
                    fixedColumns: true,
                    fixdedRightNumber: 1,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                        ]
                    ],
                });
                //为表格绑定事件
                Table.api.bindevent(school_table);
            },
            //老师列表
            teacher: function () {
                var teacher_table = $("#teacher_table");
                teacher_table.bootstrapTable({
                    url: 'facilitator/index' + location.search,
                    extend: {
                        index_url: 'facilitators/index' + location.search,
                        add_url: 'facilitators/add',
                        edit_url: 'facilitators/edit',
                        del_url: 'facilitators/del',
                        multi_url: 'facilitators/multi',
                        import_url: 'facilitators/import',
                        table: 'teacher',
                    },
                    toolbar: '#teacher_toolbar',
                    pk: 'id',
                    sortName: 'id',
                    fixedColumns: true,
                    fixdedRightNumber: 1,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                        ]
                    ],
                });
                //为表格绑定事件
                Table.api.bindevent(teacher_table);
            },
            //学生列表
            student: function () {
                var student_table = $("#student_table");
                student_table.bootstrapTable({
                    url: 'facilitator/index' + location.search,
                    extend: {
                        index_url: 'facilitators/index' + location.search,
                        add_url: 'facilitators/add',
                        edit_url: 'facilitators/edit',
                        del_url: 'facilitators/del',
                        multi_url: 'facilitators/multi',
                        import_url: 'facilitators/import',
                        table: 'student',
                    },
                    toolbar: '#student_toolbar',
                    pk: 'id',
                    sortName: 'id',
                    fixedColumns: true,
                    fixdedRightNumber: 1,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                        ]
                    ],
                });
                //为表格绑定事件
                Table.api.bindevent(student_table);
            },

        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'facilitators/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'facilitators/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'facilitators/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
